﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class AssembleLoader : MonoBehaviour
{
    void Start()
    {
        //Assembly assembly1 = Assembly.Load(dllAsset.bytes);
        //Type t = assembly1.GetType("CSharpDll");  //有命名空间时要注意  
        //MethodInfo method = t.GetMethod("Add");
        //System.Object[] p = new System.Object[] { 1, 2 };
        //print(method.Invoke(t, p));

        //Assembly assembly2 = Assembly.Load(dllAsset.bytes);
        //Type t2 = assembly2.GetType("MonoDll");
        //gameObject.AddComponent(t2);

        StartCoroutine(Test());
    }


    IEnumerator Test()
    {
        yield return new WaitForEndOfFrame();
        string url = @"file://" + Application.dataPath + "/Dll/myassets.android";
        print(url);
        WWW www = new WWW(url);

        // Wait for download to complete
        yield return www;
        yield return new WaitForSeconds(2f);
        if (www.error != null)
        {
            Debug.Log(www.error);
        }
        if (www.isDone)
        {
            #region load dll
            //Assembly assemble = System.Reflection.Assembly.Load(((TextAsset)www.assetBundle.mainAsset).bytes);
            //foreach (var i in assemble.GetTypes())
            //{
            //    //调试代码
            //    Debug.Log(i.Name);
            //}
            #endregion
            
            // Load and retrieve the AssetBundle
            TextAsset txtAsset = www.assetBundle.mainAsset as TextAsset;
            Assembly asm = Assembly.Load(txtAsset.bytes);
            foreach (var item in asm.GetTypes())
            {
                Debug.Log(item.Name);

                if (item.Name == "CSharpDll")//获取对应的Class
                {
                    foreach (var meth in item.GetMethods())//获取该类下的方法
                    {
                        Debug.Log(meth.Name);
                        if (meth.Name == "Add")//调用Add方法
                        {
                            print(meth.Invoke(null, new object[] { 1, 2 }));
                        }
                    }
                }
                if (item.Name == "MonoDll")
                {
                    GameObject go = new GameObject();
                    Component com = go.AddComponent(item);
                    MethodInfo method =  item.GetMethod("Add");
                    object result =  method.Invoke(com, new object[] { 2, 3 });
                    Debug.Log("result:"+result.ToString());
                    
                    MemberInfo[] minss = item.GetMembers(BindingFlags.Instance |
                                                         BindingFlags.Static |
                                                         BindingFlags.Public |
                                                         BindingFlags.NonPublic |
                                                         BindingFlags.DeclaredOnly);    //获取所有成员
                    foreach (var i in minss)
                    {
                        Debug.Log(i);
                    }
                }
            }
        }
    }

}
